package br.edu.unifei.mestrado.mem.graph;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import br.edu.unifei.mestrado.commons.graph.NodeWrapper;
import br.edu.unifei.mestrado.commons.partition.AbstractPartitionIndex;
import br.edu.unifei.mestrado.commons.partition.NodeCache;

public class PartitionIndexMem extends AbstractPartitionIndex {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	/**
	 * Estrutura de dados para armazenar os nós de cada partição.
	 */
	private Map<Integer, NodeCache> sets = new HashMap<Integer, NodeCache>();

	public PartitionIndexMem(int k) {
		super();
		for (int id = 0; id < k; id++) {
			sets.put(id, new NodeCache(id));
		}
	}

	@Override
	protected void addToSet(int setId, NodeWrapper node) {
		NodeCache set = sets.get(setId);
		if (set != null) {
			set.addNode(node);
		} else {
			logger.error("ERRO: SET ERRADO PARA ADICIONAR O VERTICE " + node);
		}
	}

	@Override
	protected void removeFromSet(int setId, NodeWrapper node) {
		NodeCache set = sets.get(setId);
		if (set != null) {
			set.removeNode(node);
		} else {
			logger.error("ERRO: SET ERRADO PARA REMOVER O VERTICE " + node);
		}
	}

	@Override
	public Iterable<NodeWrapper> queryNodes(String key, Integer setId) {
		NodeCache set = sets.get(setId);
		if (set != null) {
			return set.getNodes();
		} else {
			throw new RuntimeException("ERRO: SET " + setId + " ERRADO PARA QUERY");
		}
	}
	
	@Override
	public boolean containsKey(long idNode) {
		for (NodeCache set : sets.values()) {
			if (set.containsNode(idNode)) {
				return true;
			}
		}
		return false;
	}
	
}
